#    Copyright (c) 2010 Abhishek Patnia (patnia@isi.edu) and Tarang Desai (tarangde@usc.edu)
#
#    Permission is hereby granted, free of charge, to any person obtaining a copy
#    of this software and associated documentation files (the "Software"), to deal
#    in the Software without restriction, including without limitation the rights
#    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#    copies of the Software, and to permit persons to whom the Software is
#    furnished to do so, subject to the following conditions:
#
#    The above copyright notice and this permission notice shall be included in
#    all copies or substantial portions of the Software.
#
#    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#    THE SOFTWARE.

from __future__ import division
import gflags as flags

FLAGS=flags.FLAGS

def createCorpus(engTestFN, hindTestFN):
    """
        Create a corpus of English and Hindi sentences for the given filenames
    """
    e_sents = {}
    for line in open(engTestFN):
        num = int(line.strip().split('>')[0].split('=')[1]) # its a number
        sent = line.strip().split('>')[1].split('<')[0]
        e_sents[num] = sent

    h_sents = {}
    for line in open(hindTestFN):
        num = int(line.strip().split('>')[0].split('=')[1]) # its a number
        sent = line.strip().split('>')[1].split('<')[0]
        h_sents[num] = sent
    return (e_sents, h_sents)

def align(e, h):
    """
        Align the given English and Hindi sentences for the baseline criteria
    """
    alignment = []
    r = len(e)
    k = len(h)
    # case 1 (same length) and case 2 (eng. sent. is longer)
    if r == k or r > k:
        for i in range(r):
            alignment.append([i])

        # case 3 (hindi sent. is longer)
    if r < k:
        for i in range(r - 1):
            alignment.append([i])
        alignment.append(range(r - 1, k))
    return alignment

if __name__ == '__main__':

    # Flags
    # English data file
    flags.DEFINE_string('engTestFN', 'data/test/test.e', 'English test data filename')
    # Hindi data file
    flags.DEFINE_string('hindTestFN', 'data/test/test.h', 'Hindi test data filename')
    # Output filename
    flags.DEFINE_string('output', 'results/test.baseline.wa', 'Best alignments for the test sentences')

    e_sents, h_sents = createCorpus(FLAGS.engTestFN, FLAGS.hindTestFN)

    wa_file = open(FLAGS.output, 'w')
    for key in e_sents.keys():
        alignment = align(e_sents[key].split(' '), h_sents[key].split(' '))
        for e, mappings in enumerate(alignment):
            for h in mappings:
                wa_file.write('%s %s %s\n' % (key, e, h))
    wa_file.flush()
    wa_file.close()
